use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(JobBatches::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(JobBatches::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(JobBatches::Name).string().not_null())
                    .col(ColumnDef::new(JobBatches::TotalJobs).integer().not_null())
                    .col(ColumnDef::new(JobBatches::PendingJobs).integer().not_null())
                    .col(ColumnDef::new(JobBatches::FailedJobs).integer().not_null())
                    .col(
                        ColumnDef::new(JobBatches::FailedJobIds)
                            .custom(Alias::new("LONGTEXT"))
                            .not_null(),
                    )
                    .col(ColumnDef::new(JobBatches::Options).custom(Alias::new("MEDIUMTEXT")))
                    .col(ColumnDef::new(JobBatches::CancelledAt).integer())
                    .col(ColumnDef::new(JobBatches::CreatedAt).integer().not_null())
                    .col(ColumnDef::new(JobBatches::FinishedAt).integer())
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(JobBatches::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum JobBatches {
    Table,
    Id,
    Name,
    TotalJobs,
    PendingJobs,
    FailedJobs,
    FailedJobIds,
    Options,
    CancelledAt,
    CreatedAt,
    FinishedAt,
}
